package com.apple.service.impl;

import com.apple.common.CustomException;
import com.apple.dto.DishDto;
import com.apple.entity.Dish;
import com.apple.entity.DishFlavor;
import com.apple.entity.Setmeal;
import com.apple.mapper.DishMapper;
import com.apple.service.DishFlavorService;
import com.apple.service.DishService;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
    /**
     * 新增菜品，同时保存对应的口味数据
     * @param dishDto
     * */
    @Transactional
    @Override
    public void saveWithFlavor(DishDto dishDto) {
        if (dishDto == null) {
            throw new IllegalArgumentException("dishDto cannot be null");
        }

        this.save(dishDto);
        Long dishId = dishDto.getId(); // 获取dishId

        if (dishId == null) {
            throw new IllegalStateException("dishId cannot be null after saving the dish");
        }

        List<DishFlavor> flavors = dishDto.getFlavors();

        if (flavors != null && !flavors.isEmpty()) {
            flavors = flavors.stream().map(item -> {
                item.setDishId(dishId);
                return item;
            }).collect(Collectors.toList());

            // 批量保存到dish_flavor表
            dishFlavorService.saveBatch(flavors);
        }
    }
    /**
     * 根据id查询菜品信息，同时查询对应的口味信息
     * @param id
     * @return
     * */
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //查询菜品基本信息，从dish表查询
        Dish dish = this.getById(id);
        if (dish == null) {
            throw new RuntimeException("Dish为空，id: " + id);
        }
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);
        //查询当前菜品对应的口味信息，从dish_flavor表查询
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, dish.getId());
        List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);
        dishDto.setFlavors(flavors);
        return dishDto;

    }
    /**
     * 更新菜品信息，同时更新对应的口味信息
     * @param dishDto
     * @return
     * */
    @Override
    public void updateWithFlavor(DishDto dishDto) {
        // 更新dish表基本信息
        this.updateById(dishDto);
        //清理当前菜品对应口味数据--dish_flavor表的delete操作
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, dishDto.getId());
        dishFlavorService.remove(queryWrapper);
        // 添加当前提交过来的口味数据--dish_flavor表的insert操作
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map(item -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(flavors);
    }
    /**
     * 删除菜品，同时删除对应的口味信息
     * 在删除前检查菜品是否在售，在售则不允许删除
     * @param ids 要删除的菜品ID列表
     */
    @Transactional
    @Override
    public void removeWithFlavor(List<Long> ids) {
        LambdaQueryWrapper<Dish> dishQueryWrapper = new LambdaQueryWrapper<>();
        dishQueryWrapper.in(Dish::getId, ids).eq(Dish::getStatus, 1);
        int count = this.count(dishQueryWrapper);
        if (count > 0) {
            throw new CustomException("部分菜品正在售卖中，无法删除");
        }
        this.removeByIds(ids);
        LambdaQueryWrapper<DishFlavor> flavorQueryWrapper = new LambdaQueryWrapper<>();
        flavorQueryWrapper.in(DishFlavor::getDishId, ids);
        dishFlavorService.remove(flavorQueryWrapper);
    }



}
